package com.toutou.base.bindhelper

import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.google.android.material.tabs.TabLayout
import com.toutou.base.R

class TabLayoutBDAdapter {
    companion object {
        private const val TAG = "TabLayoutBDAdapter"

        //        修改选中文字和未选中文字的大小
        @JvmStatic
        @BindingAdapter(
            value = ["tabTextSize", "tabSelectedTextSize", "selectColor", "unSelectColor"],
            requireAll = false
        )
        fun setTabTextSize(
            tabLayout: TabLayout,
            tabTextSize: Int?,
            tabSelectedTextSize: Int?,
            selectColor: Int?,
            unSelectColor: Int?,
        ) {
//            自定义每一个tab的布局
            tabLayout.tabRippleColor = ColorStateList(
                arrayOf(intArrayOf()),
                intArrayOf(0x00000000)
            )

            tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(selectTab: TabLayout.Tab?) {
                    for (i in 0 until tabLayout.tabCount) {
                        val tab = tabLayout.getTabAt(i)
                        if (tab != null) {
                            if (tab.customView == null) {
                                tab.setCustomView(R.layout.layout_tab)
                            }
                            val textView =
                                tab.customView!!.findViewById<TextView>(R.id.tab_title)
                            textView.text = tab.text
                            textView.setTextColor(
                                if (tab == selectTab)
                                    selectColor ?: Color.parseColor("#323233")
                                else
                                    unSelectColor ?: Color.parseColor("#606266")
                            )
                            textView.setTypeface(
                                null,
                                if (tab == selectTab) Typeface.BOLD else Typeface.NORMAL
                            )
                            textView.setTextSize(
                                TypedValue.COMPLEX_UNIT_SP,
                                (if (tab == selectTab)
                                    tabSelectedTextSize ?: 14
                                else
                                    tabTextSize ?: 12).toFloat()
                            )
                            val icon =
                                tab.customView!!.findViewById<View>(R.id.tab_icon)
                            icon.visibility = if (tab == selectTab) View.VISIBLE else View.INVISIBLE
                        }
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {

                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })

        }

        //    加载状态变化
        @JvmStatic
        @BindingAdapter(value = ["shadowView", "loadEnd"], requireAll = false)
        fun setLoadEnd(tabLayout: TabLayout, shadowView: View?, loadEnd: Boolean) {
//            loadEnd为false的时候也要做判断，防止界面不需要接口请求的时候 eg.ShopRankDetailActivity
            tabLayout.postDelayed({
                if (shadowView != null) {
                    val tabLayoutWidth = tabLayout.width
                    var totalTabWidth = 0

                    for (i in 0 until tabLayout.tabCount) {
                        val tab = tabLayout.getTabAt(i)
                        if (tab != null) {
                            val tabView: View = tab.view
                            totalTabWidth += tabView.width
                        }
                    }

                    shadowView.visibility =
                        if (totalTabWidth > tabLayoutWidth) View.VISIBLE else View.GONE
                }
            }, 300)//要延迟判断，防止是不需要接口请求的界面，还没绘制 计算结果为0
        }
    }

}